<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 主体区域 -->
    <el-card class="box-card">
      <!-- 搜索框 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="Goods.query">
            <el-button slot="append" icon="el-icon-search" @click="Cxun"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="GoodsAdd">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 主体区域商品列表 -->
      <el-row>
        <el-col>
          <el-table :data="GoodsList" border style="width: 100%">
            <el-table-column type="index" label="#" width="50"></el-table-column>
            <el-table-column label="商品名称" prop="goods_name"></el-table-column>
            <el-table-column label="商品价格（元）" prop="goods_number" width="100"></el-table-column>
            <el-table-column label="商品重量" prop="goods_weight" width="150"></el-table-column>
            <el-table-column label="创建时间" prop="upd_time" width="120"></el-table-column>
            <el-table-column label="操作" width="200">
              <template>
                <el-button type="primary" icon="el-icon-edit" circle></el-button>
                <el-button type="danger" icon="el-icon-delete" circle @click="open"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>

      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.Goods.pagenum"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="this.Goods.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取数据列表参数项
      Goods: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每条显示多少数据
        pagesize: 1
      },
      // 保存查询到的所有数据
      GoodsList: [],
      // 保存总数据条数
      total: 0
    }
  },
  created() {
    this.getGoods()
  },
  methods: {
    // 获取数据列表
    async getGoods() {
      const { data: res } = await this.$http.get('goods', {
        params: this.Goods
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取商品列表失败')
      this.$message.success('获取商品列表成功')
      this.GoodsList = res.data.goods
      this.total = res.data.total
      console.log(res)
    },
    // 分页pageSize 改变时会触发
    handleSizeChange(newpagesize) {
      this.Goods.pagesize = newpagesize
      this.getGoods()
    },
    // 分页pagenum改变时会触发
    handleCurrentChange(newpagenum) {
      this.Goods.pagenum = newpagenum

      this.getGoods()
    },
    // 查询数据
    async Cxun() {
      const { data: res } = await this.$http.get('goods', {
        params: this.Goods
      })
      if (res.meta.status !== 200)
        return this.$message.error('查询商品列表失败')
      this.$message.success('查询商品列表成功')
      this.getGoods()
    },
    // 点击删除按钮删除数据
    async open() {
      console.log(this.GoodsList[0].goods_id)
      const { data: res } = await this.$http.delete(
        `goods/${this.GoodsList[0].goods_id}`
      )
      console.log(res)
      if (res.meta.status !== 200)
        return this.$message.error('删除商品列表失败')
      this.$message.success('删除商品列表成功')
      this.getGoods()
    },
    // 点击添加商品弹出框
    GoodsAdd() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
</style>